package com.mew.user.events;

import com.mew.user.events.service.UserSignService;
import com.mew.common.exception.ResponseCode;
import com.mew.common.rest.events.SignInfoResponseMessage;
import com.mew.common.rest.events.SignResponseMessage;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * @Date 2020/10/21 0021
 **/
@RestController
@Slf4j
public class UserSignController {

    @Autowired
    private UserSignService userSignService;

    @GetMapping(value = "/sign/info/{uid}")
    @ResponseBody
    public SignInfoResponseMessage signInfo(@PathVariable("uid") long uid){
     try {
         return userSignService.signInfo(uid);
     }catch (Exception e){
         log.error("signInfo error uid: "+uid, e);
         return SignInfoResponseMessage.builder().build().buildByResponseCode(ResponseCode.SysException);
     }

    }

    @PostMapping(value = "/sign/signandinfo")
    @ResponseBody
    public SignInfoResponseMessage signAndInfo(@RequestParam("uid") long uid){
        try {
            SignInfoResponseMessage message = userSignService.signInfo(uid);
            userSignService.sign(uid, message);
            return message;
        }catch (Exception e){
            log.error("signInfo error uid: "+uid, e);
            return SignInfoResponseMessage.builder().build().buildByResponseCode(ResponseCode.SysException);
        }

    }

    /**
     * 签到
     * @param uid
     * @return
     */
    @GetMapping(value = "/sign/{uid}")
    @ResponseBody
    public SignResponseMessage sign(@PathVariable("uid") long uid){
        try {
            return userSignService.sign(uid);
        }catch (Exception e){
            log.error("sign error uid: "+uid, e);
            return SignResponseMessage.builder().build().buildByResponseCode(ResponseCode.SysException);
        }


    }

    /**
     * 领取
     * @param uid
     * @param gtype
     * @return
     */
    @PostMapping(value = "/sign/gain")
    @ResponseBody
    public SignResponseMessage gain(@RequestParam("uid") long uid, @RequestParam("gtype") int gtype){
        try {
            return userSignService.gain(uid, gtype);
        }catch (Exception e){
            log.error("gain error uid: "+uid, e);
            return SignResponseMessage.builder().build().buildByResponseCode(ResponseCode.SysException);
        }
    }

    @GetMapping(value = "/sign/repair/{uid}/{dayNum}")
    @ResponseBody
    public SignResponseMessage repair(@PathVariable("uid") long uid, @PathVariable("dayNum") int dayNum){
        try {
            return userSignService.signDay(uid, dayNum);
        }catch (Exception e){
            log.error("signDay error uid: "+uid, e);
            return SignResponseMessage.builder().build().buildByResponseCode(ResponseCode.SysException);
        }
    }


}
